﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Spelprojekt
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Guitario : Microsoft.Xna.Framework.Game
    {
        Texture2D backgroundTexture;
        Texture2D PlayerTexture;
        Texture2D Ghost1Texture;
        Texture2D Ghost2Texture;
        Texture2D Bana1Texture;
        Texture2D Bana2Texture;
        GraphicsDevice device;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        int screenWidth;
        int screenHeight;
        int laneCount=0;
        Player Gura;
        
       
        public Guitario()
        {
            
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;

            Gura = new Player();

            backgroundTexture = Content.Load<Texture2D>("Background");

            Bana1Texture = Content.Load<Texture2D>("Bana1");
            Bana2Texture = Content.Load<Texture2D>("Bana2");

            PlayerTexture = Content.Load<Texture2D>(Gura.PlayerPicture);

            screenWidth = device.PresentationParameters.BackBufferWidth;
            screenHeight = device.PresentationParameters.BackBufferHeight;
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            KeyboardState Keystate = Keyboard.GetState();

            PlayerTexture = Content.Load<Texture2D>(Gura.PlayerPicture);
            Gura.Move();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            DrawStart();
            DrawLane();
            DrawPlayer();
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        private void DrawStart()
        {
            Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
            spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
            
        }

        private void DrawPlayer()
        {
            
            spriteBatch.Draw(PlayerTexture, Gura.PosXY, Color.White);

        }

        private void DrawLane()
        {
            if (laneCount <= 0)
            {
                Rectangle Bana1Rectangle = new Rectangle(0, 0, screenWidth, screenHeight);
                spriteBatch.Draw(Bana1Texture, Bana1Rectangle, Color.White);
            }
            if (laneCount < 1)
            {
                Rectangle Bana1Rectangle = new Rectangle(0, 0, screenWidth, screenHeight);
                spriteBatch.Draw(Bana1Texture, Bana1Rectangle, Color.White);
            }

            if (Gura.PosXY.X < 0)
            {
                laneCount++;
            }
        }

        public Rectangle Playa { get; set; }
    }
}
